/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.djdb.shop.dao;

import be.djdb.shop.Phone;


import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;
import javax.validation.ConstraintViolationException;

/**
* @author Lieven Roegiers
* @copyright 2013
* @from MY_SHOP
*/

public class PhoneDAO implements Serializable{
    private final EntityManager em;
    public PhoneDAO(EntityManager em) {
        this.em = em;
    }
    public List<Phone> findAllPhones   () throws DAOException{
        try{
            List<Phone> results = em.createNamedQuery("Phone.findAll", Phone.class).getResultList();
            return results;
        } catch (PersistenceException ex) {
            ERROR_Control(ex, "phone.cannot.findALLPhones.errormsg");
        }
        return null;
    }
    public Phone findPhone(long nr) throws DAOException{
        try{
            TypedQuery<Phone> q = em.createNamedQuery("Phone.findByNr", Phone.class);
            q.setParameter("nr", nr);
            return q.getSingleResult();
        } catch (NoResultException ex) {
            Logger.getLogger(Phone.class.getName()).log(Level.WARNING, ex.getMessage(), ex);
            return null;
        } catch (PersistenceException ex) {
            ERROR_Control(ex, "phone.cannot.findPhone.errormsg");
        }
        return null;
    }
    public Phone get(int id) throws DAOException{
        try{
            TypedQuery<Phone> q = em.createNamedQuery("Phone.findById", Phone.class);
            q.setParameter("id", id);
            return q.getSingleResult();
        } catch (NoResultException ex) {
            Logger.getLogger(Phone.class.getName()).log(Level.WARNING, ex.getMessage(), ex);
            return null;
        } catch (PersistenceException ex) {
            ERROR_Control(ex, "phone.cannot.findPhone.errormsg");
        }
        return null;
    }
    public boolean isexist(String nr) throws DAOException{
        try{ 
            TypedQuery<Long> q = em.createNamedQuery("Phone.checkTitle", Long.class);
            q.setParameter("title",nr);
            return q.getSingleResult() == 0;
        } catch (PersistenceException ex) {
            ERROR_Control(ex, "phone.cannot.checkifexist.errormsg");
        }
        return false;
    }
    public void addPhone(Phone p) throws DAOException{
       try {
            em.getTransaction().begin();
            em.persist(p);        
            em.getTransaction().commit();
        } catch (ConstraintViolationException | PersistenceException ex) {
            String message = ex.getMessage();
            Throwable nestedException = ex;
            while (nestedException != null) {
                if (nestedException instanceof ConstraintViolationException) {
                    //message = Util.buildErrorMessage((ConstraintViolationException)nestedException);
                    break;
                }
                nestedException = nestedException.getCause();
            }

            ERROR_Control(ex, "phone.cannot.add.errormsg");
        }
    }
    public void updatePhone(Phone p) throws DAOException{
        throw new UnsupportedOperationException("Not supported yet.");
    }
    public void removePhone(long id) throws DAOException{
        try {
            em.getTransaction().begin();
            Phone r = em.find(Phone.class, id);
            if (r != null) {
                em.remove(r);
            }
            em.getTransaction().commit();
        } catch (PersistenceException ex) {
            ERROR_Control(ex, "product.cannot.remove.errormsg");
        } 
    }
    public void removePhone(Phone p) throws DAOException{
        removePhone(p.getId());
    }
    private void ERROR_Control(PersistenceException ex, String phoneerrormsg) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    private void ERROR_Control(java.lang.RuntimeException ex, String phoneerrormsg) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
